﻿using System;
using System.Collections.Generic;
using System.Data;
using Jin.Core.DataProviders;


namespace Jin.Core.Query
{
    /// <summary>
    /// 查询参数
    /// </summary>
    public class QueryParameter
    {
        /// <summary>
        /// 参数默认数据输入输出类型
        /// </summary>
        internal const ParameterDirection DefaultParameterDirection = ParameterDirection.Input;
        /// <summary>
        /// 参数默认size
        /// </summary>
        internal const int DefaultSize = 50;
        #region 参数的一些属性定义
        private ParameterDirection _mode = DefaultParameterDirection;

        private int _size = DefaultSize;
        public int Sacle { get; set; }
        public int Precision { get; set; }

        /// <summary>
        /// 获取或这是大小
        /// </summary>
        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }
        /// <summary>
        ///获取或这是参数输出类型
        /// </summary>
        public ParameterDirection Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }
        /// <summary>
        /// 获取或设置参数名称
        /// </summary>
        public string ParameterName { get; set; }
        /// <summary>
        /// 获取或设置参数的值
        /// </summary>
        public object ParameterValue{ get; set; }
        /// <summary>
        /// 或者或设置参数的类型
        /// </summary>
        public DbType DataType { get; set; }
 

        #endregion
        //public QueryParameter(string parameterName,object parameterValue,ParameterDirection mode,DbType dbType)
        //{
        //    ParameterName = parameterName;
        //    ParameterVaule = parameterValue;
        //    _mode = mode;
        //    DataType = dbType;
        //}


    }
    /// <summary>
    /// 查询命令类
    /// </summary>
   public class QueryCommand
   {
       #region 一些参数定义
 
       private int commandTimeout = 60;
       /// <summary>
       /// 命令执行过期时间
       /// </summary>
       public int CommandTimeout
       {
           get { return commandTimeout; }
           set { commandTimeout = value; }
       }
       private QueryParameterCollection parameters;
       /// <summary>
       /// 获取或设置参数集
       /// </summary>
       public QueryParameterCollection Parameters
       {
           get { return parameters; }
           set { parameters = value; }
       }
       /// <summary>
       /// 输出参数集
       /// </summary>
       public List<object> OutputValues;
       /// <summary>
       /// 数据驱动程序
       /// </summary>
       public IDataProvider Provider { get; set; }
       /// <summary>
       /// 数据驱动程序名称
       /// </summary>
       public string ProviderName { get; set; }
       /// <summary>
       /// 数据库操作类型
       /// </summary>
       public CommandType CommandType { get; set; }
       /// <summary>
       /// 获取或设置执行的sql
       /// </summary>
       public string CommandSql { get; set; }
       #endregion
       public QueryCommand(string sql,IDataProvider provider)
       {

           CommandSql = sql;
           CommandType = CommandType.Text;
           Parameters = new QueryParameterCollection();
           OutputValues = new List<object>();
           Provider = provider;
       }

        #region 一些方法
       /// <summary>
       /// 是否有输出参数
       /// </summary>
       /// <returns></returns>
       public bool HasOutputParams()
       {
           bool bOut = false;
           foreach(QueryParameter param in Parameters)
           {
            if(param.Mode!=ParameterDirection.Input)
            {
                bOut = true;
                break;
            }
           }
           return bOut;
       }

       #region 添加参数


       /// <summary>
       /// 添加sql参数
       /// </summary>
       /// <param name="parameterName">参数名称</param>
       /// <param name="parameterValue">参数值</param>
       /// <param name="maxSize">最大值</param>
       /// <param name="dbType">参数数据类型</param>
       /// <param name="diretion">参数输出类型</param>
       private void AddParameter(string parameterName,object parameterValue,int maxSize,DbType dbType,ParameterDirection diretion)
       {
           if (parameters == null)
               parameters = new QueryParameterCollection();
           QueryParameter param = new QueryParameter
           {
               ParameterName = parameterName,
               ParameterValue = (parameterValue ?? DBNull.Value),
               Mode = diretion,
               DataType = dbType
           };
           if (maxSize > -1 && diretion != ParameterDirection.Output)
               param.Size = maxSize;
           parameters.Add(param);
       }
       /// <summary>
       /// 添加sql参数
       /// </summary>
       /// <param name="parameterName">参数名称</param>
       /// <param name="parameterValue">参数值</param>
       /// <param name="dbType">参数数据类型</param>
       /// <param name="diretion">参数输出类型</param>
       public void AddParameter(string parameterName, object parameterValue, DbType dbType, ParameterDirection diretion)
       {
           AddParameter(parameterName, parameterValue,QueryParameter.DefaultSize, dbType, diretion);
       }
       /// <summary>
       /// 添加sql参数
       /// </summary>
       /// <param name="parameterName">参数名称</param>
       /// <param name="parameterValue">参数值</param>
       /// <param name="dbType">参数数据类型</param>
       public void AddParameter(string parameterName, object parameterValue, DbType dbType)
       {
           AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dbType, QueryParameter.DefaultParameterDirection);

       }
       /// <summary>
       /// 添加sql参数
       /// </summary>
       /// <param name="parameterName">参数名称</param>
       /// <param name="parameterValue">参数值</param>
       public void AddParameter(string parameterName, object parameterValue)
       {
           AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, DbType.Object, QueryParameter.DefaultParameterDirection);

       }
       #endregion
       #region 添加输出参数
       /// <summary>
       /// 添加输出参数
       /// </summary>
       /// <param name="parameterName">参数名</param>
       /// <param name="maxSize">最大值</param>
       /// <param name="dbType">参数数据类型</param>
       public void AddOutputParamter(string parameterName,int maxSize,DbType dbType)
       {
           AddParameter(parameterName, DBNull.Value, maxSize,dbType, ParameterDirection.Output);
       }
       /// <summary>
       /// 添加输出参数
       /// </summary>
       /// <param name="parameterName">参数名</param>
       /// <param name="dbType">参数类型</param>
       public void AddOutputParamter(string parameterName,DbType dbType)
       {
           AddOutputParamter(parameterName, -1, dbType);

       }
       /// <summary>
       /// 添加输出参数
       /// </summary>
       /// <param name="parameterName">参数名</param>
       /// <param name="maxSize">最大值</param>
       public void AddOutputParamter(string parameterName, int maxSize)
       {
           AddOutputParamter(parameterName, maxSize, DbType.AnsiString);

       }
       /// <summary>
       /// 添加输出参数
       /// </summary>
       /// <param name="parameterName">参数名</param>
       public void AddOutputParamter(string parameterName)
       {
           AddOutputParamter(parameterName,  -1);

       }

       /// <summary>
       /// 添加一个命令返回参数(返回值)
       /// </summary>
       public void AddReturnParameter()
       {
           AddParameter("@RETURN_VALUE", null, DbType.UInt32, ParameterDirection.ReturnValue);
       }
       #endregion

      // public IDbCommand ToDbCommand(IDbTransaction trannie)
      // { 
        //var connection=Provider.Fa
     //  }

        #endregion
   }
    /// <summary>
    /// 参数集合
    /// </summary>
   public class QueryParameterCollection : List<QueryParameter>
   { }

   public class QueryCommandCollection : List<QueryCommand> { }
}
